﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Nirvana
{
    /// <summary>
    /// 实时同步绑定的Animator参数脚本
    /// </summary>
    [RequireComponent(typeof(Animator))]
    [AddComponentMenu("Nirvana/Animator/Animator Synchronizer")]
    public sealed class AnimatorSynchronizer : MonoBehaviour
    {
        [SerializeField]
        [Tooltip("The source animator")]
        private Animator source;
        [SerializeField]
        private string[] booleanParams;
        [SerializeField]
        private string[] integerParams;
        [SerializeField]
        private string[] floatParams;

        private Animator curAnimator;
        public Animator Source
        {
            get
            {
                return this.source;
            }
            set
            {
                this.source = value;
            }
        }

        private int[] boolhash;
        private int[] inthash;
        private int[] floathash;

        void Awake()
        {
            this.curAnimator = GetComponent<Animator>();
            if(this.booleanParams != null)
            {
                this.boolhash = new int[this.booleanParams.Length];
                for (int i = 0; i < this.boolhash.Length; i++)
                    this.boolhash[i] = Animator.StringToHash(this.booleanParams[i]);
                this.booleanParams = null;
            }

            if (this.integerParams != null)
            {
                this.inthash = new int[this.integerParams.Length];
                for (int i = 0; i < this.inthash.Length; i++)
                    this.inthash[i] = Animator.StringToHash(this.integerParams[i]);
                this.integerParams = null;
            }

            if (this.floatParams != null)
            {
                this.floathash = new int[this.floatParams.Length];
                for (int i = 0; i < this.floathash.Length; i++)
                    this.floathash[i] = Animator.StringToHash(this.floatParams[i]);
                this.floatParams = null;
            }

        }

        void Update()
        {
            if (this.source == null || this.curAnimator == null) return;
            if (this.boolhash != null)
            {
                for(int i=0,imax = boolhash.Length; i < imax; i++)
                {
                    bool b = this.source.GetBool(boolhash[i]);
                    this.curAnimator.SetBool(boolhash[i], b);
                }
            }

            if (this.inthash != null)
            {
                for (int i = 0, imax = inthash.Length; i < imax; i++)
                {
                    int b = this.source.GetInteger(inthash[i]);
                    this.curAnimator.SetInteger(inthash[i], b);
                }
            }

            if (this.floathash != null)
            {
                for (int i = 0, imax = floathash.Length; i < imax; i++)
                {
                    float b = this.source.GetFloat(floathash[i]);
                    this.curAnimator.SetFloat(floathash[i], b);
                }
            }

        }

    }
}

